package com.itdr.protal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itdr.common.Const;
import com.itdr.common.ServerResponse;
import com.itdr.pojo.Shipping;
import com.itdr.pojo.vo.ShippingVO;
import com.itdr.protal.mapper.ShippingMapper;
import com.itdr.protal.service.IShippingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itdr.utils.VOUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author AirZhang
 * @since 2020-10-20
 */
@Service
public class ShippingServiceImpl extends ServiceImpl<ShippingMapper, Shipping> implements IShippingService {
    @Resource
    ShippingMapper shippingMapper;

    private IPage getShippingPage(IPage<Shipping> shippingIPage) {
        List li = new ArrayList();
        List<Shipping> records = shippingIPage.getRecords();
        for (Shipping s : records) {
            ShippingVO shippingVO = VOUtil.getShippingVO(s);
            li.add(shippingVO);
        }
        shippingIPage.setRecords(li);
        return shippingIPage;
    }

    @Override
    public ServerResponse addOne(Integer uid, Shipping sh) {
        // 1.地址可以重复，不要做重复判断
        // 2.判断当前用户是否有地址
        Integer a = shippingMapper.selectCount(new QueryWrapper<Shipping>().eq("user_id", uid));
        // 3.如果没有就把这条地址做为默认地址
        if (a <= 0) {
            sh.setType(1);
        } else {
            sh.setType(0);
        }
        sh.setUserId(uid);
        sh.setCreateTime(new Date());
        sh.setUpdateTime(new Date());
        int insert = shippingMapper.insert(sh);
        if (insert <= 0) {
            return ServerResponse.toFail(Const.AddressEnum.FAIL_ADD_ADDRESS.getCode(), Const.AddressEnum.FAIL_ADD_ADDRESS.getDesc());
        }

        // 4.返回成功数据，当前用户地址列表
        IPage<Shipping> shippingIPage = shippingMapper.selectPage(new Page<Shipping>(), new QueryWrapper<Shipping>().eq("user_id", uid));
        shippingIPage = getShippingPage(shippingIPage);
        return ServerResponse.toSuccess(shippingIPage);
    }

    @Override
    public ServerResponse deleteOne(Integer uid, Integer sid) {
        int i = shippingMapper.deleteById(sid);
        if (i <= 0) {
            return ServerResponse.toFail(Const.AddressEnum.FAIL_DELETE_ADDRESS.getCode(), Const.AddressEnum.FAIL_DELETE_ADDRESS.getDesc());
        }

        // 如果用户只剩下一条地址了，就把这条地址设置为默认地址
        List<Shipping> shippingList = shippingMapper.selectList(new QueryWrapper<Shipping>().eq("user_id", uid));
        if (shippingList.size() == 1) {
            if (!shippingList.get(0).getType().equals(1)) {
                shippingMapper.update(null, new UpdateWrapper<Shipping>().set("type", 1).eq("user_id", uid));
            }
        }

        // 返回成功数据，当前用户地址列表
        IPage<Shipping> shippingIPage = shippingMapper.selectPage(new Page<Shipping>(), new QueryWrapper<Shipping>().eq("user_id", uid));
        shippingIPage = getShippingPage(shippingIPage);
        return ServerResponse.toSuccess(shippingIPage);
    }

    @Override
    public ServerResponse updateOne(Integer uid, Shipping sh) {
        // 1.要修改的地址是否存在
        Shipping shipping = shippingMapper.selectById(sh.getId());
        if (shipping == null) {
            return ServerResponse.toFail(Const.AddressEnum.NO_ADDRESS.getCode(), Const.AddressEnum.NO_ADDRESS.getDesc());
        }
        // 2.修改对应信息
        sh.setUpdateTime(new Date());
        int i = shippingMapper.updateById(sh);
        if (i <= 0) {
            return ServerResponse.toFail(Const.AddressEnum.FAIL_UPDATE_ADDRESS.getCode(), Const.AddressEnum.FAIL_UPDATE_ADDRESS.getDesc());

        }
        // 返回成功数据，当前用户地址列表
        IPage<Shipping> shippingIPage = shippingMapper.selectPage(new Page<Shipping>(), new QueryWrapper<Shipping>().eq("user_id", uid));
        shippingIPage = getShippingPage(shippingIPage);
        return ServerResponse.toSuccess(shippingIPage);
    }

    @Override
    public ServerResponse selectOne(Integer uid, Integer sid) {
        Shipping shipping = shippingMapper.selectById(sid);
        if (shipping == null) {
            return ServerResponse.toFail(Const.AddressEnum.NO_ADDRESS.getCode(), Const.AddressEnum.NO_ADDRESS.getDesc());
        }
        ShippingVO shippingVO = VOUtil.getShippingVO(shipping);
        return ServerResponse.toSuccess(shippingVO);
    }

    @Override
    public ServerResponse selectAll(Integer uid, Integer sid) {
        // 返回成功数据，当前用户地址列表
        IPage<Shipping> shippingIPage = shippingMapper.selectPage(new Page<Shipping>(), new QueryWrapper<Shipping>().eq("user_id", uid));
        shippingIPage = getShippingPage(shippingIPage);
        return ServerResponse.toSuccess(shippingIPage);
    }

    @Override
    public ServerResponse setDefault(Integer uid, Integer sid) {
        // 1.查询原默认地址
        Map m = new HashMap();
        m.put("user_id", uid);
        m.put("type", 1);
        Shipping shipping = shippingMapper.selectOne(new QueryWrapper<Shipping>().allEq(m));
        // 2.设置新的默认地址
        int update = shippingMapper.update(null, new UpdateWrapper<Shipping>().set("type", 1).eq("id", sid));
        if (update <= 0) {
            return ServerResponse.toFail(Const.AddressEnum.FAIL_UPDATE_ADDRESS.getCode(), Const.AddressEnum.FAIL_UPDATE_ADDRESS.getDesc());
        }
        // 3.取消原有默认地址
        int update2 = shippingMapper.update(null, new UpdateWrapper<Shipping>().set("type", 0).eq("id", shipping.getId()));
        if (update2 <= 0) {
            return ServerResponse.toFail(Const.AddressEnum.FAIL_UPDATE_ADDRESS.getCode(), Const.AddressEnum.FAIL_UPDATE_ADDRESS.getDesc());
        }
        // 返回成功数据，当前用户地址列表
        IPage<Shipping> shippingIPage = shippingMapper.selectPage(new Page<Shipping>(), new QueryWrapper<Shipping>().eq("user_id", uid));
        shippingIPage = getShippingPage(shippingIPage);
        return ServerResponse.toSuccess(shippingIPage);
    }
}
